package com.youlin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.youlin.dto.BaseResDto;
import com.youlin.dto.PageDataResDto;
import com.youlin.dto.PageInfo;
import com.youlin.dto.Status;
import com.youlin.dto.req.EditCTemplateReqDto;
import com.youlin.entities.BygAppInfo;
import com.youlin.entities.BygCustomTemplate;
import com.youlin.mapper.BygCustomTemplateMapper;
import com.youlin.service.BygAppInfoService;
import com.youlin.service.BygCustomTemplateService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang.BooleanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.util.List;

@Service
public class BygCustomTemplateServiceImpl extends ServiceImpl<BygCustomTemplateMapper,
                                    BygCustomTemplate> implements BygCustomTemplateService {
    @Autowired
    private BygAppInfoService bygAppInfoService;

    @Override
    public PageDataResDto<List<BygCustomTemplate>> findAllcTemplates(PageInfo pageInfo) {
        LambdaQueryWrapper<BygCustomTemplate> wrapper = Wrappers.lambdaQuery(BygCustomTemplate.class)
                .orderByDesc(BygCustomTemplate::getCreateTime, BygCustomTemplate::getId);
        IPage<BygCustomTemplate> page = page(new Page<>(pageInfo.getPage(), pageInfo.getSize()), wrapper);
        pageInfo.setTotal(page.getTotal());
        return new PageDataResDto<>(Status.SUCCESS).setData(page.getRecords()).setPageInfo(pageInfo);
    }

    @Override
    public BaseResDto<Status, String> editCustomTemplate(EditCTemplateReqDto data) {
        if (BooleanUtils.isTrue(data.getDel())) {
            LambdaQueryWrapper<BygAppInfo> wrapper = Wrappers
                    .lambdaQuery(BygAppInfo.class).eq(BygAppInfo::getCustomId, data.getCTemplateId());
            if (bygAppInfoService.count(wrapper) > 0) return new BaseResDto<>(Status.CTCANOTDEL);
            if (! removeById(data.getCTemplateId())) return new BaseResDto<>(Status.ERROR);
        } else {
            LambdaQueryWrapper<BygCustomTemplate> wrapper = Wrappers
                    .lambdaQuery(BygCustomTemplate.class).eq(BygCustomTemplate::getName, data.getName());
            if (data.getCTemplateId() != null) wrapper.ne(BygCustomTemplate::getId, data.getCTemplateId());
            if (count(wrapper) > 0) return new BaseResDto<>(Status.CTNDUPLICATE);
            BygCustomTemplate customTemplate = new BygCustomTemplate()
                    .setId(data.getCTemplateId()).setName(data.getName());
            if (data.getCTemplateId() == null) customTemplate.setCreateTime(LocalDate.now());
            if (! saveOrUpdate(customTemplate)) return new BaseResDto<>(Status.ERROR);
        }
        return new BaseResDto<>(Status.SUCCESS);
    }

}
